package com.oscar.ehome.utils.dowload.db;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import com.oscar.ehome.bean.pagebean.StudyListBean;

import java.util.ArrayList;


/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/7/17
 */

public class DbDataOperation {

    public static ArrayList<StudyListBean> getBookInfo(ContentResolver resolver) {
        ArrayList<StudyListBean> bookList = new ArrayList<StudyListBean>();
        StudyListBean StudyListBean;
        Cursor cursor = resolver.query(Uri.parse(DbTags.URI_TABLE_BOOK_INFO), null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                StudyListBean = new StudyListBean();

                StudyListBean.setId(getFieldContent(cursor, DbTags.FIELD_BOOK_ID));
                StudyListBean.setIds(Integer.parseInt(getFieldContent(cursor, DbTags.FIELD_ID)));
                StudyListBean.setVideoState(Integer.parseInt(getFieldContent(cursor, DbTags.FIELD_BOOK_DOWNLOARD_STATE)));
                StudyListBean.setDownloadurl(getFieldContent(cursor, DbTags.FIELD_BOOK_DOWNLOAD));
                StudyListBean.setMediaFile(getFieldContent(cursor, DbTags.FIELD_BOOK_NAME));
                StudyListBean.setVideo_Path(getFieldContent(cursor, DbTags.FIELD_BOOK_PATH));
                StudyListBean.setSize(Integer.parseInt(getFieldContent(cursor, DbTags.FIELD_BOOK_PROGRESS)));
                StudyListBean.setSmallImg(getFieldContent(cursor, DbTags.FIELD_BOOK_IMG));
                StudyListBean.setBidSize(Integer.parseInt(getFieldContent(cursor, DbTags.FIELD_BOOK_SIZE)));
                StudyListBean.setTitle(getFieldContent(cursor, DbTags.FIELD_BOOK_TITLE));
                bookList.add(StudyListBean);
            }
            cursor.close();
        }
        return bookList;
    }

    public static void insertToBookInfo(ContentResolver resolver, StudyListBean bookinfo) {

        ContentValues values = new ContentValues();

        values.put(DbTags.FIELD_ID, bookinfo.getIds());// id
        values.put(DbTags.FIELD_BOOK_ID, bookinfo.getId());// id
        values.put(DbTags.FIELD_BOOK_DOWNLOAD, bookinfo.getDownloadurl());
        values.put(DbTags.FIELD_BOOK_DOWNLOARD_STATE, bookinfo.getVideoState());
        values.put(DbTags.FIELD_BOOK_NAME, bookinfo.getMediaFile());
        values.put(DbTags.FIELD_BOOK_PATH, bookinfo.getVideo_Path());
        values.put(DbTags.FIELD_BOOK_PROGRESS, bookinfo.getSize());
        values.put(DbTags.FIELD_BOOK_IMG, bookinfo.getSmallImg());
        values.put(DbTags.FIELD_BOOK_TITLE, bookinfo.getTitle());
        values.put(DbTags.FIELD_BOOK_SIZE, bookinfo.getBidSize());
        resolver.insert(Uri.parse(DbTags.URI_TABLE_BOOK_INFO), values);// review
    }

    /*
     * 更新图书是否是下载状态 0 为非下载状态 1为下载状态
	 */
    public static void updateDownloadState(ContentResolver contentResolver, int state, String bookName) {
        ContentValues values = new ContentValues();
        values.put(DbTags.FIELD_BOOK_DOWNLOARD_STATE, state);
        contentResolver.update(Uri.parse(DbTags.URI_TABLE_BOOK_INFO), values, DbTags.FIELD_BOOK_NAME + "=?",
                new String[]{bookName + ""});
    }

    public static StudyListBean queryBook(ContentResolver resolver, String tag, String key) {
        StudyListBean StudyListBean = null;
        Cursor cursor = resolver.query(Uri.parse(DbTags.URI_TABLE_BOOK_INFO), null, tag + "=?",
                new String[]{key + ""}, null);

        if (cursor != null) {
            if (cursor.moveToNext()) {
                StudyListBean = new StudyListBean();
                StudyListBean.setId(getFieldContent(cursor, DbTags.FIELD_BOOK_ID));
                StudyListBean.setDownloadurl(getFieldContent(cursor, DbTags.FIELD_BOOK_DOWNLOAD));
                StudyListBean.setVideoState(Integer.parseInt(getFieldContent(cursor, DbTags.FIELD_BOOK_DOWNLOARD_STATE)));
                StudyListBean.setMediaFile(getFieldContent(cursor, DbTags.FIELD_BOOK_NAME));
                StudyListBean.setVideo_Path(getFieldContent(cursor, DbTags.FIELD_BOOK_PATH));
                StudyListBean.setSize(Integer.parseInt(getFieldContent(cursor, DbTags.FIELD_BOOK_PROGRESS)));
                StudyListBean.setTitle(getFieldContent(cursor, DbTags.FIELD_BOOK_TITLE));
                StudyListBean.setBidSize(Integer.parseInt(getFieldContent(cursor, DbTags.FIELD_BOOK_SIZE)));
                StudyListBean.setSmallImg(getFieldContent(cursor, DbTags.FIELD_BOOK_IMG));
            }
            cursor.close();
        }
        return StudyListBean;
    }


    public static String getFieldContent(Cursor cursor, String fieldName) {
        return cursor.getString(cursor.getColumnIndex(fieldName));
    }

}
